package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.service.FileDealWithService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by yw on 2018/10/29.
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/filedealwith/")
public class FileDealWithController {
    private static Logger log = LoggerFactory.getLogger(FileDealWithController.class);

    @Autowired
    private FileDealWithService fileDealWithService;

    @PostMapping("upload")
    public String upload (@RequestParam(value="file") MultipartFile file) throws IOException {
        try{
            String host = "127.0.0.1";
            int port = 21;
            String username = "feilong";
            String password = "Fl123456";
            String basePath = "/home/feilong";
            String filePath = "";
            String filename;
            String fileName=file.getOriginalFilename();
            String newfileName = fileName.substring(fileName.lastIndexOf("."));
            FileInputStream in = (FileInputStream) file.getInputStream();
            newfileName = fileDealWithService.genImageName()+newfileName;
            boolean rs =fileDealWithService.uploadFile(host,port,username,password,basePath,filePath,newfileName,in);
            String data = "http://47.100.46.40:443/" + newfileName;
            if(rs){
                return data;
            }
            return "false";
        }catch (Exception e){
            log.error("错误为：e:{}"+e);
        }
        return "";
    }

}
